/**
 * 
 */
package specialtriggers;

import java.io.FileWriter;
import java.io.PrintWriter;

import bot.IRCBot;
import trigger.TriggerFactory;
import trigger.VariableTrigger;

/**
 * @author Philip
 *
 */
public class NewTriggerTrigger extends VariableTrigger {
	private IRCBot ircBot;
	private TriggerFactory tf;
	private String input;

	public NewTriggerTrigger(String trigger, String response, IRCBot ircBot, TriggerFactory tf) {
		super(trigger, response, 1);
		this.ircBot = ircBot;
		this.tf = tf;
	}

	public String response(int i) {
		String triggerText = input.substring(input.indexOf("add: ") + 5);
		System.out.println(triggerText);
		try {
			ircBot.addTrigger(tf.createTrigger(triggerText));

			PrintWriter outputWriter = new PrintWriter(new FileWriter("triggers.txt", true));
	        outputWriter.println(triggerText);
	        outputWriter.close();
		} catch (Exception e) {
			return "Inkorrekt syntax!!!! :<<< (" + e.getMessage() + ")";
		}
		return super.response(i);
	}
	
	public void updateVariables(String nickName, String sender, String receiver, String senderHostname, String input) {
		this.input = input;
		super.updateVariables(nickName, sender, receiver, senderHostname, input);
	}
}
